// mdsa.cpp
// multi dimensional static array

#include "auxlib.h"
#include <iostream>
using namespace std;
#include "mdsa.h"

void fillMatrixMdsa(double p[MDSA_SIZE][MDSA_SIZE], 
				int r, int c, double d) 
{
	for (int i=0 ; i < r ; i++) {
		for (int j=0 ; j < c ; j++) {
			p[i][j] = d;
		}
	}
}

void addToMatrixMdsa(double p[MDSA_SIZE][MDSA_SIZE], 
				 int r, int c, double d) 
{
	for (int i=0 ; i < r ; i++) {
		for (int j=0 ; j < c ; j++) {
			p[i][j] += d;
		}
	}
}

void multidimensional_static_array() 
{

	cout << "=== multidimensional_static_array, size=" 
		<< MDSA_SIZE << endl;


	double eps = 1e-8;
	MyClock clk;
	double duration_sec;
	clk.markTime();
	// code to test begin (1)
	// large variables must be in global scope, not to produce stack overflow
	static double p[MDSA_SIZE][MDSA_SIZE];

	int row = MDSA_SIZE;
	int col = MDSA_SIZE;
	fillMatrixMdsa(p,row,col,1.1);
	duration_sec = clk.getDiffTime();
	cout << "alloc & fill matrix=" << duration_sec << " sec" << endl;
	// code to test begin (2)
	clk.markTime();
	addToMatrixMdsa(p,row,col,2.2);
	duration_sec = clk.getDiffTime();
	cout << "addToMatrix=" << duration_sec << " sec" << endl;
	// code to test end (2)

	// confirm that add was done
	myAssert(nearEqual(p[2][3],3.3,eps));
}
